package com.netease.nimlib.ipc.provider.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.netease.nimlib.ipc.cp.client.IContentObserver;
import com.netease.nimlib.ipc.cp.client.ProviderClient;

public class MainActivity extends AppCompatActivity {

    private final static String NAME = "nim";
    private final static String KEY = "test";
    private final static String DEF_VAL = "huangjun";

    private IContentObserver observer = new IContentObserver() {
        @Override
        public void onChange(String name, String key, int type, Object newValue) {
            Log.i("huangjun", "onChange=" + name + "/" + key + "/" + type + "/" + newValue);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ProviderClient.get(this, NAME).registerObserver(KEY, ProviderClient.UriTypeEnum.String, observer);
    }

    public void test1(View view) {
        try {
            long startTime = System.currentTimeMillis();
            String s = ProviderClient.get(this, NAME).queryString(KEY, DEF_VAL);
            Log.i("huangjun", "result=" + s + ", cost=" + (System.currentTimeMillis() - startTime));
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public void test2(View view) {
        try {
            ProviderClient.get(this, NAME).updateString(KEY, "val_" + System.currentTimeMillis(), true);
            ProviderClient.get(this, NAME).invoke(KEY, "invoke_params");
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public void test3(View view) {
        try {
            ProviderClient.get(this, NAME).removeKey(KEY);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}
